(function($) {
	$.getUrlParam = function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}
})(jQuery);



const d = D = {
	queryOne() {
		var id = $.getUrlParam("did");
		$.ajax({
			type: 'get',
			url: url.path + 'dingdan/queryOne',
			data: {
				id: id
			},
			success: function(res) {
				var res = eval("(" + res + ")");
				if (res.code == "200") {
					$('#woNumber').text(res.data.dno);
					$('#contactNameDetail').text(res.data.lxrname);
					$('#contactPhoneDetail').text(res.data.lxrphone);
					$('#faultLocationDetail').text(res.data.address);
					$('#faultReasonDetail').text(res.data.description);
					$('#faultImage1').attr('src', res.data.img1);
					$('#faultImage2').attr('src', res.data.img2);
					$('#faultImage3').attr('src', res.data.img3);
					$('#repairDateDetail').text(D.timestampToDate(res.data.date));
					$('#faultLocationDetail').text(res.data.address);
				} else {
					alert(res.msg);
					$('#captchaContainer').data('sliderCaptcha').reset();
					$('#captchaContainer').data('sliderCaptcha').init();
					$('#xyb').attr('disabled', true).css({
						'background-color': '#ccc',
						'cursor': 'not-allowed',
						'border-color': '#ccc'
					});
				}
			}
		});
	},
	timestampToDate(timestamp) {
		const date = new Date(timestamp); // 将时间戳转换为Date对象
		const options = {
			year: 'numeric',
			month: 'long',
			day: 'numeric'
		}; // 定义日期格式
		return new Intl.DateTimeFormat('zh-CN', options).format(date); // 使用Intl.DateTimeFormat进行格式化
	},

	queryWeixiu() {
		var wid = $('#dep_disabledSelect').val();
		$.ajax({
			type: 'get',
			url: url.path + 'dingdan/queryWeixiu',
			data: {
				id: wid
			},
			success: function(res) {
				var res = eval("(" + res + ")");
				if (res.code == "200") {
					$('#wid').val(res.data.wid);
					$('#wname').val(res.data.wname);
					$('#wphone').val(res.data.wphone);
				} else {
					alert(res.msg);
				}
			}
		});
	},
	wlist() {
		let _tbody = $('#dep_disabledSelect');
		$.ajax({
			type: 'get',
			url: url.path + 'wei/lise',

			success: function(res) {
				console.log('res-->', res)
				var res = eval("(" + res + ")");
				if (res.code == 200) {

					_tbody.empty(); // 将容器清空
					let list = res.data;
					let tr = $(`<option value="0">===请选择===</option>`)
					_tbody.append(tr);
					$(list).each(function(i, e) {

						let _tr = $(`<option value="` + e.wid + `">` + e.wname +
							`</option>`)
						_tbody.append(_tr);
					});

				} else {
					alert('error:' + res.msg);
				}
			}
		});
	},
	edit(state) {
		$.ajax({
			url: url.path + 'dingdan/edit',
			data: {
				id: $.getUrlParam("did"),
				state: state,
				wid: $('#wid').val(),
				wname: $('#wname').val(),
				wphone: $('#wphone').val(),
				enddate: "",
				senddate: ""
			},
			type: 'post',
			dataType: 'json',
			success: function(res) {
				if (res.code == 200) {

					if (state == 3) {
						window.location.href = "jd_chulizhong.html?did=" + $.getUrlParam("did");
					} else if (state == 7) {
						window.location.href = "yiquxiao.html?did=" + $.getUrlParam("did");
					}
				}
			},
			error: function() {
				alert('修改失败')
			}
		})

	}
}

$(function() {
	D.queryOne();

	$('#assignButton').click(function() {
		$('#wname').val("").val("");
		$('#wphone').val("").val("");
		$('#wid').val("").val("0");
		D.wlist();
		$('#myModal').modal('show');
		$("#did").val($.getUrlParam("did"));
		// 当dep_disabledSelect的值改变时，触发queryWeixiu函数
		$('#dep_disabledSelect').change(function() {
			D.queryWeixiu();
		});
	});

	$('#tianjia').click(function() {
		D.edit(3);
	})

	$('#cancelButton').click(function() {
		D.edit(7)
	})
})